package com.example.service.impl;

import com.example.mapper.ContentcategoryMapper;
import com.example.pojo.ContentCategory;
import com.example.pojo.ContentCategoryExample;
import com.example.req.ContentCategoryReq;
import com.example.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.print.attribute.standard.OrientationRequested;
import java.util.List;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Autowired
    private ContentcategoryMapper contentcategoryMapper;

    @Override
    public List<ContentCategory> queryAll() {
        return contentcategoryMapper.selectByExample(null);
    }

    @Override
    public List<ContentCategory> queryByCondition(ContentCategoryReq contentCategoryReq) {
        ContentCategoryExample example = new ContentCategoryExample();
        ContentCategoryExample.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(contentCategoryReq.getName())){
            criteria.andNameLike("%" + contentCategoryReq.getName() + "%");
        }
        return contentcategoryMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(ContentCategory contentCategory) {
        if (contentCategory.getId() == null){
            return contentcategoryMapper.insert(contentCategory);
        }else {
            return contentcategoryMapper.updateByPrimaryKey(contentCategory);
        }
    }

    @Override
    public int deleteById(Long id) {
        return contentcategoryMapper.deleteByPrimaryKey(id);
    }
}
